# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=libavif
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.9.0
pkgrel=3
pkgdesc="Library for encoding and decoding .avif files (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://github.com/AOMediaCodec/libavif"
license=('BSD-2-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-aom"
         "${MINGW_PACKAGE_PREFIX}-dav1d"
         "${MINGW_PACKAGE_PREFIX}-rav1e"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-gdk-pixbuf2"
             "${MINGW_PACKAGE_PREFIX}-nasm"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/AOMediaCodec/libavif/archive/v${pkgver}.tar.gz"
        002-pixbuf-install-repfix.patch)
sha512sums=('220ef27edf716af4ffc5b035c63f84e2848f165a3fe9c1b74dc0ba29f0de65147610fd4266f1b2419f60cfbb72159368da840fcb574c7a07364a60548d3db179'
            '60f8bad03460695a094a6ffabbe33f839239fef2bae99d9be7f3a3f9cfde14b694d0c68dce6cc987e9c865a0d97638e951d55abcbab4cc554719803e5d0bb2d2')

prepare() {
  cd ${_realname}-${pkgver}

  # https://gitlab.kitware.com/cmake/cmake/-/issues/19632
  patch -p1 -i ${srcdir}/002-pixbuf-install-repfix.patch
}

build() {
  [[ -d ${srcdir}/build-${CARCH} ]] && rm -rf ${srcdir}/build-${CARCH}
  mkdir -p build-${CARCH} && cd build-${CARCH}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G Ninja \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DAVIF_BUILD_APPS=ON \
    -DAVIF_CODEC_AOM=ON \
    -DAVIF_CODEC_DAV1D=ON \
    -DAVIF_CODEC_RAV1E=ON \
    -DAVIF_BUILD_GDK_PIXBUF=ON \
    -DAVIF_BUILD_TESTS=ON \
    -DAVIF_ENABLE_WERROR=OFF \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build ./
}

check() {
  cd ${srcdir}/build-${CARCH}
  ${MINGW_PREFIX}/bin/ctest ./ || true
}

package() {
  cd ${srcdir}/build-${CARCH}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --build ./ --target install

  install -Dm644 ${srcdir}/${_realname}-${pkgver}/README.md ${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}/README.md
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
